//
//  STAddAndEditMemorailController.swift
//  SlowTrajectory
//
//  Created by iOS-Czz on 2023/10/23.
//

import UIKit
import WMZDialog
class STAddAndEditMemorailController: STProjectBaseController {
    
    @IBOutlet var briefView: UIView!
    @IBOutlet var dateView: UIView!
    @IBOutlet var wayView: UIView!
    @IBOutlet var remindView: UIView!
    var model:STMemorailModel?
    var isAdd = true
    @IBOutlet var nameTF: UITextField!
    @IBOutlet var wayTF: UITextField!
    @IBOutlet var remindTF: UITextField!
    
    @IBOutlet var dateTF: UITextField!
    var way = 0
    var remind = 0
    override func viewDidLoad() {
        super.viewDidLoad()
        setNavStyle()
        if !self.isAdd {
            setPageData()
        }
        // Do any additional setup after loading the view.
    }
    func setPageData(){
        self.way = self.model!.way
        self.remind = self.model!.remind
        self.nameTF.text = self.model?.name
        self.dateTF.text = self.model?.date
        self.wayTF.text = getWay(self.model!.way)
        self.remindTF.text = getRemind(self.model!.remind)
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(false, animated: true)
    }
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        self.briefView.addDashedBorder(color: .gray, lineWidth: 2, cornerRadius: 15)
        self.dateView.addDashedBorder(color: .gray, lineWidth: 2, cornerRadius: 15)
        self.wayView.addDashedBorder(color: .gray, lineWidth: 2, cornerRadius: 15)
        self.remindView.addDashedBorder(color: .gray, lineWidth: 2, cornerRadius: 15)
    }
    func setNavStyle(){
        self.title = self.isAdd ? "纪念日添加" : "纪念日编辑"
        self.addRightBarButton(imageName: "icon_save")
    }
    override func clockRightBarButtonAction() {
        self.view.endEditing(true)
        if self.nameTF.text!.isEmpty {
            UIView.showTipsView("请简短的描述这个纪念日")
            return
        }
        if self.dateTF.text!.isEmpty {
            UIView.showTipsView("请选择这个纪念日的开始日期")
            return
        }
        if self.wayTF.text!.isEmpty {
            UIView.showTipsView("请选择这个纪念日的方式")
            return
        }
        if self.remindTF.text!.isEmpty {
            UIView.showTipsView("请选择提醒这个纪念日")
            return
        }
        var model = STMemorailModel()
        if !self.isAdd {
            model = self.model!
        }
        
        model.name = self.nameTF.text!
        model.way = self.way
        model.remind = self.remind
        model.date = self.dateTF.text!
        model.isDefault = false
        model.saveToDB()
        UIView.showResultView(self.isAdd ? "纪念日添加成功" : "纪念日编辑成功", true)
        if self.isAdd {
            let helper = STMemorailModel.getUsingLKDBHelper()
            helper.delete(with: STMemorailModel.self, where: "isDefault = true")
        }else{
            model.updateToDB()
        }
        let delay = 1.5 // 2秒
        let dispatchTime = DispatchTime.now() + delay
        DispatchQueue.main.asyncAfter(deadline: dispatchTime) {
            self.navigationController?.popToRootViewController(animated: true)
        }
    }
    @IBAction func clickSelectDateAction(_ sender: Any) {
        self.view.endEditing(true)
        let dialog = Dialog()
        dialog.wMainToBottomSet()(true)
            .wTypeSet()(DialogTypeCalander)
            .wOpenChineseDateSet()(false)
            .wDateTimeTypeSet()("yyyy-MM-dd")
            .wEventOKFinishSet()({(anyId:Any?,otherData:Any?)in
                let selectDay = anyId as! String
                self.dateTF.text = selectDay.toDate(format: "yyyy-MM-dd")?.toString(format: "yyyy-MM-dd")
            })
        _ = dialog.wStart()
    }
    @IBAction func clickSelectWayAction(_ sender: Any) {
        self.view.endEditing(true)
        let dialog = Dialog()
        dialog
        .wTypeSet()(DialogTypeSheet)
        .wCancelTitleSet()("Cancel")
        .wDataSet()(["年计算","月计算","周计算"])
        .wEventFinishSet()({(anyID:Any?,path:IndexPath?,type:DialogType) in
            self.wayTF.text = anyID as? String
            self.way = path!.row
        })
        _ = dialog.wStart()
        
    }
    @IBAction func clickRemindAction(_ sender: Any) {
        self.view.endEditing(true)
        let dialog = Dialog()
        dialog
        .wTypeSet()(DialogTypeSheet)
        .wDataSet()(["关闭","1天前","3天前","1周前"])
        .wEventFinishSet()({(anyID:Any?,path:IndexPath?,type:DialogType) in
            self.remindTF.text = anyID as? String
            self.remind = path!.row
        })
        _ = dialog.wStart()
        
    }
    
    func getWay(_ way:Int) -> String {
        let ways = ["年计算","月计算","周计算"]
        return ways[way]
    }
    func getRemind(_ remind:Int) -> String {
        let reminds = ["关闭","1天前","3天前","1周前"]
        return reminds[remind]
    }
}
